var search = function (nums, target) {
    if (!nums || !nums.length) {
        return -1
    }
    return binarySearch(nums, 0, nums.length-1, target);
    function binarySearch(nums, start, end, target) {
        if(start+1 > end){
            return  -1;
        }
        if(start+1 === end){
            if(nums[end] === target) {
                return end;
            }
            if(nums[start] === target) {
                return start;
            }
        }
        let mid = Math.floor(start + (end - start) / 2);
        if (nums[mid] === target){
            return  binarySearch(nums,mid,end,target);
        }
        if (nums[mid] < target){
            return  binarySearch(nums,mid+1,end,target);
        }
        return  binarySearch(nums,start,mid - 1,target);
    }
};

console.log(search([1,2,2,4,5,5],2))

